package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdInfoMapper;
import com.bwie.mapper.TbTagMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdInfo;
import com.bwie.pojo.TbTag;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.R;
import com.bwie.vo.ProdInfoVo;
import com.bwie.vo.ProdVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 31439
* @description 针对表【tb_prod(商品信息表)】的数据库操作Service实现
* @createDate 2025-07-22 09:48:21
*/
@Service
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper tbProdMapper;
    @Autowired
    TbTagMapper tbTagMapper;

    @Autowired
    TbProdInfoMapper tbProdInfoMapper;

    @Override
    public R getProdList(ProdVo prodVo) {
        Page<TbProd> page = new Page<>(prodVo.getPageNum(), prodVo.getPageSize());
        Page<TbProd> prodPage = tbProdMapper.getProdList(page,prodVo);
        return R.OK(prodPage);
    }

    @Override
    public R addProdList(TbProd tbProd) {
        TbProd prod = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdName, tbProd.getProdName()));
        if(prod != null){
            return R.ERROR(500,"该商品已存在");
        }
        tbProd.setCreateTime(new Date());
        tbProdMapper.insert(tbProd);
        return R.OK();
    }

    @Override
    public R getTag() {
        List<TbTag> tbTag = tbTagMapper.selectList(null);
        return R.OK(tbTag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteBatchProd(List<Integer> ids) {
        //批量删除商品
        tbProdMapper.deleteBatchIds(ids);
        return R.OK();
    }

    @Override
    public R getProdInfoList() {
        List<TbProdInfo> list = tbProdInfoMapper.selectList(null);
        return R.OK(list);
    }

    @Override
    public R getProdById(Integer prodId) {
        //判断
        TbProd tbProd = getById(prodId);
        if(tbProd == null){
            return R.ERROR(500,"商品不存在");
        }

        return R.OK(tbProd);
    }

    @Override
    public R getProdInforList(ProdInfoVo prodInfoVo) {
        Page<TbProdInfo> page = new Page<>(prodInfoVo.getPageNum(), prodInfoVo.getPageSize());

        QueryWrapper<TbProdInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbProdInfo::getProdId,prodInfoVo.getProdId())
                        .orderByAsc(TbProdInfo::getProdId);
        Page<TbProdInfo> tbProdInfoPage = tbProdInfoMapper.selectPage(page, wrapper);
        return R.OK(tbProdInfoPage);
    }

    @Override
    public R addProdInfo(TbProdInfo tbProdInfo) {
        //查询商品是否存在
        TbProd tbProd = getById(tbProdInfo.getProdId());
        if(tbProd == null){
            return R.ERROR(500,"商品不存在");
        }
        //判断SKU是否存在
        QueryWrapper<TbProdInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbProdInfo::getProdId,tbProdInfo.getProdId())
                        .eq(TbProdInfo::getProdName,tbProdInfo.getProdName());

        TbProdInfo one = tbProdInfoMapper.selectOne(wrapper);
        if(one != null){
            return R.ERROR(500,"商品SKU已存在");
        }

        //添加商品信息
        tbProdInfoMapper.insert(tbProdInfo);

        return R.OK();
    }
}




